<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class Assemble extends NotORM {

    //设置表名
    protected function getTableName($id) {
        return 'team_found';
    }

    // 获取某个商品正在拼团列表
    public function getAssembleList( $goods_id ) {
        return $this->getORM()
            ->select('user_id,found_id,found_end_time,team_id,nickname,head_pic,`join`,need,(need - `join`) as few,goods_id')
            ->where( array('goods_id'=>$goods_id,'status'=>1) )
            ->order('found_time DESC')
            ->fetchAll();
    }

    // 获取某个商品正在拼团列表
    public function getAssembleListNum( $goods_id ) {
        $total = $this->getORM()
            ->where( array('goods_id'=>$goods_id,'status'=>1) )
            ->count('found_id');
        return intval($total);
    }

    // 获取某个商品正在拼团列表
    public function getAssembleDetail( $user_id ) {
        return $this->getORM()
            ->select('found_id,found_end_time,team_id,nickname,head_pic,`join`,need,(need - `join`) as few,goods_id')
            ->where( array( 'goods_id'=>$user_id ) )
            ->fetchAll();
    }

    // 获取某个商品正在拼团的团长信息
    public function getAssembleFound( $found_id ) {
        return $this->getORM()
            ->select('found_id,found_end_time,team_id,nickname,head_pic,`join`,need,(need - `join`) as few,goods_id,status')
            ->where( array( 'found_id' =>$found_id ) )
            ->fetchOne();
    }

    // 获取某个商品正在拼团的团员信息
    public function getAssembleFollow( $found_id ) {
        $orm = \PhalApi\DI()->notorm->team_follow;
        return $orm
            ->select('follow_user_nickname,follow_user_head_pic,follow_user_id')
            ->where( array( 'found_id' =>$found_id ) )
            ->order('follow_time DESC')
            ->fetchAll();
    }

    // 获取某个商品正在拼团的商品信息(团长)
    public function getAssembleGoods( $user_id, $found_id ) {
        $time = time();
        $sql = 'SELECT og.goods_name, og.goods_num, og.spec_key_name, og.final_price,og.goods_images '
            . 'FROM tp_order_goods AS og LEFT JOIN tp_team_found AS f '
            . 'ON og.order_id = f.order_id '
            . 'WHERE f.found_id = '.$found_id.' AND f.user_id = '.$user_id
            . ' ORDER BY og.add_time DESC';
        return $this->getORM()->queryAll($sql, array());
    }

    // 获取某个商品正在拼团的商品信息(团员)
    public function getAssembleGoodsFollow( $user_id, $found_id ) {
        $time = time();
        $sql = 'SELECT og.goods_name, og.goods_num, og.spec_key_name, og.final_price,og.goods_images '
            . 'FROM tp_order_goods AS og LEFT JOIN tp_team_follow AS f '
            . 'ON og.order_id = f.order_id '
            . 'WHERE f.found_id = '.$found_id.' AND f.follow_user_id = '.$user_id
            . ' ORDER BY og.add_time DESC';
        return $this->getORM()->queryAll($sql, array());
    }

}
